//
//  TSQNManager.swift
//  ThreeStone
//
//  Created by 王磊 on 1/29/17.
//  Copyright © 2017 ThreeStone. All rights reserved.
//

import UIKit

class CMDBQNManager: NSObject {
    
    static let getInstance: CMDBQNManager = CMDBQNManager()
    
    private override init() {
        super.init()
    }
}

extension CMDBQNManager: CMDBQNManagerDelegate {
    
    func queryQNToken(handler: CMDBQNTokenHandler, error: CMDBHttpErrorHandler, faliure: CMDBHttpFailureHandler) {
        let qnTokenParam = CMDBQNTokenParam()
        
        CMDBHttpManager.getInstance.request(qnTokenParam.URLString(), param: nil, success: { (sysTime, data, page) in
            
            handler(token: data as! String)
            
            }, error: error, failure: faliure)
        
    }
    
    
    
//    func uploadImagesToQN(uploadParam: CMDBQNUploadParam, progressHandler: CMDBQNUploadProgressHandler, qnUrlHandler: CMDBQNuUploadHandler) throws {
//        
//        guard !uploadParam.filePath.isEmpty else {
//            
//            throw CMDBQNControllerExceptionType.ExceptionCode(code: CMDBQNControllerExceptionCodeType.FilePathInvalid)
//        }
//        
//        guard !uploadParam.qnToken.isEmpty else {
//            
//            throw CMDBQNControllerExceptionType.ExceptionCode(code: CMDBQNControllerExceptionCodeType.QNTokenInvalid)
//        }
//        
//        let manager: QNUploadManager = QNUploadManager()
//        
//        let uploadOption: QNUploadOption = QNUploadOption(mime: nil,progressHandler: { (key ,percent) in
//            
//            if percent == 1.0 {
//                
//                UIImage.qn_RemoveImageAtPath(uploadParam.imageName)
//                
//                progressHandler(percent: percent ,uploadStatus: CMDBUploadStatus.Complete)
//                
//            } else if percent == 0.0 {
//                
//                progressHandler(percent: percent ,uploadStatus: CMDBUploadStatus.Begin)
//                
//            } else {
//                
//                progressHandler(percent: percent ,uploadStatus: CMDBUploadStatus.Proceed)
//            }
//            
//            },params: nil,checkCrc: false,cancellationSignal: {
//                
//                return false
//        })
//        
//        let iconKey: String = "\(uploadParam.uToken)\(NSDate.timeStamp_ms())"
//        
//        manager.putFile(uploadParam.filePath ,key:iconKey, token: uploadParam.qnToken,complete: { (info,key,resp) in
//            
//            qnUrlHandler(qnUrl: uploadParam.qn_Flag ? CMDB_QN_Domain_String + key : key)
//            
//            },option: uploadOption)
//        
//    }

    
    
    
    func uploadImageToQN(uploadParam: CMDBQNUploadParam, progressHandler: CMDBQNUploadProgressHandler, qnUrlHandler: CMDBQNuUploadHandler) throws {
        
        guard !uploadParam.filePath.isEmpty else {
            
            throw CMDBQNControllerExceptionType.ExceptionCode(code: CMDBQNControllerExceptionCodeType.FilePathInvalid)
        }
        
        printLog(uploadParam.filePath)
        
        guard !uploadParam.qnToken.isEmpty else {
            
            throw CMDBQNControllerExceptionType.ExceptionCode(code: CMDBQNControllerExceptionCodeType.QNTokenInvalid)
        }
        
        let manager: QNUploadManager = QNUploadManager()
        
        let uploadOption: QNUploadOption = QNUploadOption(mime: nil,progressHandler: { (key ,percent) in
            
            printLog("\(key) \(percent)")
            
            if percent == 1.0 {
                
//                UIImage.qn_RemoveImageAtPath(uploadParam.imageName)
                
                progressHandler(percent: percent ,uploadStatus: CMDBUploadStatus.Complete)
                
            } else if percent == 0.0 {
                
                progressHandler(percent: percent ,uploadStatus: CMDBUploadStatus.Begin)
                
            } else {
                
                progressHandler(percent: percent ,uploadStatus: CMDBUploadStatus.Proceed)
            }
            
            },params: nil,checkCrc: false,cancellationSignal: {
                
                return false
        })
        
        let iconKey: String = "\(uploadParam.uToken)\(NSDate.timeStamp_ms())"
        
        manager.putFile(uploadParam.filePath ,key:iconKey, token: uploadParam.qnToken,complete: { (info,key,resp) in
            printLog(info.statusCode)
            
            qnUrlHandler(qnUrl: uploadParam.qn_Flag ? CMDB_QN_Domain_String + key : key)
            
            },option: uploadOption)
        
    }
}
